<?php


namespace app\common\model;


use think\Model;
/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2022/3/25 4:50 下午
 *@说明:优惠券模型
 */
class Coupon extends Model
{
    protected $pk = "id";
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    //修改format类型
    protected $dateFormat = "Y-m-d H:i:s";
    protected $type = [
        'money'   => 'float',
        'day'   => 'float',
    ];
    public function getPkAttr($value,$data) {
        return $this->pk;
    }
    public function user(){
        return $this->hasOne(Users::class, 'user_id','user_id');
    }
//    public function lucky(){
//        return $this->hasOne(Lucky::class, 'id','lucky_id');
//    }
    public function getTimeTextAttr($value,$data) {
        return date('Y-m-d',$data['add_time']);
    }
    public function getEndTimeTextAttr($value,$data) {
        return date($this->dateFormat,$data['end_time']);
    }
    public function getStatusTextAttr($value,$data) {
        if ($data['status']==-1){
            if (time()>$data['end_time']){
                return "已过期";
            }
            $status = [-1=>'未使用',1=>'已使用'];
            return $status[$data['status']];
        }else{
            $status = [-1=>'未使用',1=>'已使用'];
            return $status[$data['status']];
        }
    }
    public function getUseTimeTextAttr($value,$data) {
        if ($data['use_time']){
            return date($this->dateFormat,$data['use_time']);
        }
        return "---";
    }
}
